# This file creates Table 2
setwd("/Users/ericguntermann/Documents/Papers/Representation of Party Preferences/Replication")
library(foreign)
library(xtable)

dat <- read.dta("criteria.dta")
criteria <- dat
criteria2 <- dat[-which(dat$countryyear %in% c("Thailand 2001", "Thailand 2011","Japan 1996", "Japan 2013")),]

#Criterion 1

tab1 <- data.frame(round(mean(criteria2$propfic[criteria2$proportional==0])*100,1), round(mean(criteria2$propfic[criteria2$proportional==1])*100,1), round(mean(criteria$propfic)*100,1))
colnames(tab1) <- c("Non-PR", "PR", "Overall")
rownames(tab1) <- c("Proportion most liked in cabinet (%)")

#Criterion 2
tab2 <- data.frame(t(round(prop.table(with(criteria2, table(mostliked==FALSE, proportional)),2)*100,1)[1,]), round(prop.table(table(criteria$mostliked==TRUE))[2]*100,1)) 
colnames(tab2) <-c("Non-PR", "PR", "Overall")

#Criterion 3
tab3 <- data.frame(round(mean(criteria2$lddiff[criteria2$proportional==0]),2), round(mean(criteria2$lddiff[criteria2$proportional==1]),2), round(mean(criteria$lddiff),2))
colnames(tab3) <- c("Non-PR", "PR", "Overall")
rownames(tab3) <- c("Evaluation of governing vs. opposition parties")


taball <- as.matrix(rbind(tab1, tab2, tab3))
rownames(taball)[2] <- "Most liked party overall in cabinet"

xtab <- xtable(taball, caption="Performance of PR and non-PR Systems on Three Criteria")
sink("taball.tex")
print(xtab,caption.placement = getOption("xtable.caption.placement", "top"), table.placement = getOption("xtable.table.placement", "H"), only.contents=T)
sink(NULL)